package com.seari.utils;

import com.seari.bean.User;
import com.seari.pojo.QueryUser;
import com.seari.service.UserService;

import io.jsonwebtoken.Claims;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

public class ParseUtils {
    /**
     * token解析，返回uid
     * @param request
     * @return
     */
    public static String getUidFromToken(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Claims claim = JwtTokenUtils.phaseToken(token);
        return claim.get("uid").toString();
    }

    /**
     * 解析token ,返回uid
     * @param request
     * @param userService
     * @return
     */
    public static String getUidFromToken(HttpServletRequest request, UserService userService) {
//        String token = request.getHeader("Authorization");
//        Claims claim = JwtUtil.parseJWT(token);
//        String stationName = claim.get("stationName")+"";
//        QueryUser qu = new QueryUser();
//        qu.setStationName(stationName);
//        qu.setUserName(claim.getSubject());
        QueryUser qu = new QueryUser();
        qu.setStationName("testName");
        qu.setUserName("testName");
        List<User> userList = userService.getUserByParams(qu);
        if(userList!=null&&userList.size()>0){
            return userList.get(0).getUid()+"";
        }else{
            return "";
        }
    }
}
